#pragma once
#include "AnimImage.h"

vector<SDL_Rect> AnimImage::InitVector(int Frames)
{
	vector<SDL_Rect> InitVec(Frames);
	AnimBox = InitVec;
	FrameCount = AnimBox.size() - 1;
	return AnimBox;
}

SDL_Rect AnimImage::AllocFrame(int FrameNumber, int X, int Y, int W, int H)
{
	SDL_Rect Src;
	Src.x = X;	Src.y = Y;
	Src.w = W;	Src.h = H;

	return AnimBox[FrameNumber] = Src;
}

SDL_Rect AnimImage::AutoAllocFrame(int Columns, int Rows)
{
	PixPtr FrameSize(Surface->w/Rows, Surface->h/Columns);
	Uint8 WorkingFrame = 0;
	AnimBox.resize(Columns*Rows);
	FrameCount = AnimBox.size() - 1;
	
	for(int y = 0; y <= Columns; y++)
		for(int x = 0; x <= Rows; x++)
			AllocFrame(WorkingFrame, x*FrameSize.X, y*FrameSize.Y, FrameSize.X, FrameSize.Y);

	return AnimBox[WorkingFrame];
}

void AnimImage::CurrentFrameIncr()
{
	if(CurrentFrame == AnimBox.size() - 1)
		CurrentFrame = 0;
	else
		CurrentFrame++;
}

void AnimImage::Draw(int X, int Y)
{
	Box.x = X;
	Box.y = Y;

	SDL_BlitSurface(Surface, &AnimBox.at(CurrentFrame), E_VP, &Box);
	CurrentFrameIncr();
}

void AnimImage::Draw(PixPtr Pos)
{
	Box.x = Pos.X;
	Box.y = Pos.Y;

	SDL_BlitSurface(Surface, &AnimBox.at(CurrentFrame), E_VP, &Box);
	CurrentFrameIncr();
}

void AnimImage::DrawFrame(int X, int Y, int FrameNum)
{
	Box.x = X;
	Box.y = Y;

	SDL_BlitSurface(Surface, &AnimBox[FrameNum], E_VP, &Box);
}

void AnimImage::DrawFrame(PixPtr Pos, int FrameNum)
{
	Box.x = Pos.X;
	Box.y = Pos.Y;

	SDL_BlitSurface(Surface, &AnimBox[FrameNum], E_VP, &Box);
}

AnimImage::AnimImage()
{
	InitVector(0);
	Surface = NULL;
}

AnimImage::AnimImage(string FileName, int Columns, int Rows)
{
	InitVector(Columns*Rows);
	Image::LoadSurface(FileName);
	FrameCount = AnimBox.size() - 1;
	FrameSize.Set(Surface->w/Rows, Surface->h/Columns);
}